/**
 * Created with JetBrains WebStorm.
 * User: rekey
 * Date: 7/9/13
 * Time: 21:14
 * To change this template use File | Settings | File Templates.
 */
var mongoose = require('mongoose');
var when = require('when');

var schemas = require('../schemas/index.js');
var schema = schemas['post'];

schema.virtual('formatTime').get(function() {
    return {
        year : this.created.getFullYear(),
        month : this.created.getMonth() + 1,
        date : this.created.getDate()
    };
});

var model = mongoose.model('post', schema);
var categoryDao = require('./category.js');

function create(data) {
    var deferred = when.defer();
    var Model = new model();
    Object.keys(data).forEach(function(key) {
        Model[key] = data[key];
    });
    Model.save(function(err, doc) {
        if (err) {
            deferred.reject(err);
        } else {
            deferred.resolve(doc);
        }
    });
    return deferred.promise;
}

function getOneBySlug(slug) {
    var deferred = when.defer();
    var query = model.findOne({
        slug : slug
    });
    query.populate('category');
    query.populate('author');
    query.populate('media');
    query.exec(function(err, post) {
        if (err) {
            deferred.reject(err);
        } else {
            deferred.resolve(post);
        }
    });
    return deferred.promise;
}

function getList(page, limit) {
    var t = Date.now();
    page = page - 1;
    limit = limit || 10;
    var deferred = when.defer();
    var query = model.find();
    query.sort({
        created : -1
    });
    query.limit(limit);
    query.skip(page * limit);
    query.populate('category');
    query.populate('author');
    query.populate('media');
    query.exec(function(err, posts) {
        if (err) {
            deferred.reject(err);
        } else {
            deferred.resolve(posts);
            console.log('Posts getList 查询费时' + (Date.now() - t) + 'ms');
            t = null;
        }
    });
    return deferred.promise;
}

function getListByCategorySlug(slug, page, limit) {
    var t = Date.now();
    page = page - 1;
    limit = limit || 20;
    return categoryDao.getOneBySlug(slug).then(function(category) {
        var categoryId = category._id;
        var deferred = when.defer();
        var query = model.find({
            category : categoryId
        });
        query.sort({
            created : -1
        });
        query.limit(limit);
        query.skip(page * limit);
        query.populate('category');
        query.populate('author');
        query.populate('media');
        query.exec(function(err, posts) {
            if (err) {
                deferred.reject(err);
            } else {
                deferred.resolve(posts);
                console.log('Posts getList 查询费时' + (Date.now() - t) + 'ms');
                t = null;
            }
        });
        return deferred.promise;
    });
}

function getCount() {
    var deferred = when.defer();
    model.count(function(err, count) {
        if (err) {
            deferred.reject(err);
        } else {
            deferred.resolve(count);
        }
    });
    return deferred.promise;
}

function getCountByCategorySlug(slug) {
    return categoryDao.getOneBySlug(slug).then(function(category) {
        var categoryId = category._id;
        var deferred = when.defer();
        model.count({
            category : categoryId
        }, function(err, count) {
            if (err) {
                deferred.reject(err);
            } else {
                deferred.resolve(count);
            }
        });
        return deferred.promise;
    });
}

module.exports = {
    getOneBySlug : getOneBySlug,
    create : create,
    getList : getList,
    getListByCategorySlug : getListByCategorySlug,
    getCount : getCount,
    getCountByCategorySlug : getCountByCategorySlug
};